package com.cskaoyan.gateway.controller.shopping;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.shopping.ICartService;
import com.mall.shopping.IContentService;
import com.mall.shopping.IProductCateService;
import com.mall.shopping.IProductService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.dto.*;
import com.mall.user.annotation.Anoymous;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.intercepter.TokenInterceptor;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;


@RestController
@RequestMapping("shopping")
public class ItemController {


    @Reference
    IProductCateService iProductCateService;
    @Reference
    IProductService iProductService;
    @Reference
    IContentService contentService;

    @Reference(retries = 0)
    ICartService iCartService;


    /**
     * 获得所有产品类别
     *
     * @return {@link ResponseData}
     */
    @Anoymous
    @GetMapping("categories")
    public ResponseData getAllProductCategories() {
        AllProductCateRequest request = new AllProductCateRequest();
        request.setSort("sortOrder");
        AllProductCateResponse response = iProductCateService.getAllProductCate(request);
        if (response.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtil().setData(response.getProductCateDtoList());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    /**
     * 得到的产品细节
     *
     * @param id id
     * @return {@link ResponseData}
     */
    @Anoymous
    @GetMapping("product/{id}")
    public ResponseData getProductDetail(@PathVariable("id") Long id) {
        ProductDetailRequest request = new ProductDetailRequest();
        request.setId(id);
        ProductDetailResponse response = iProductService.getProductDetail(request);
        if (response.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtil().setData(response.getProductDetailDto());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    /**
     * 得到导航
     *
     * @return {@link ResponseData}
     */
    @Anoymous
    @GetMapping("/navigation")
    public ResponseData getNavigation() {
        NavListResponse response = contentService.queryNavList();

        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())) {
            return new ResponseUtil().setData(response.getPannelContentDtos());
        }

        return new ResponseUtil().setErrorMsg(response.getMsg());
    }


    /**
     * 查询推荐商品
     *
     * @return
     */
    @Anoymous
    @GetMapping("recommend")
    public ResponseData getRecommendGoods() {

        RecommendResponse response = iProductService.getRecommendGoods();
        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())) {
            return new ResponseUtil().setData(response.getPanelContentItemDtos());
        }

        return new ResponseUtil().setErrorMsg(response.getMsg());
    }


    /**
     * 添加购物车
     *
     * @param map
     * @return {@link ResponseData}
     */
    @PostMapping("carts")
//	public ResponseData addCarts(@RequestBody AddCartRequest addCartRequest){
    public ResponseData addCarts(@RequestBody Map<String, String> map) {
        // add  by  王得昭
        String productId = map.get("productId");
        String productNum = map.get("productNum");
        String userId = map.get("userId");
        AddCartRequest addCartRequest = new AddCartRequest();
        addCartRequest.setItemId(Long.valueOf(productId));
        addCartRequest.setUserId(Long.valueOf(userId));
        addCartRequest.setNum(Integer.valueOf(productNum));
        // add  by  王得昭


        AddCartResponse response = iCartService.addToCart(addCartRequest);
        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())) {
            return new ResponseUtil().setData("成功");
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }


    /**
     * 商品列表
     *
     * @param productRequest 产品要求
     * @return {@link ResponseData}
     */
    @RequestMapping("goods")
    @Anoymous
    public ResponseData goodsList(AllProductRequest productRequest) {

        AllProductResponse allProduct = iProductService.getAllProduct(productRequest);
        AllProductDto allProductDto = new AllProductDto();

        if (ShoppingRetCode.SUCCESS.getCode().equals(allProduct.getCode())) {
            allProductDto.setData(allProduct.getProductDtoList());
            allProductDto.setTotal(allProduct.getTotal());
            return new ResponseUtil().setData(allProductDto);
        }

        return new ResponseUtil().setErrorMsg(allProduct.getMsg());
    }


    /**
     * 购物车列表
     *
     * @param request
     * @return {@link ResponseData}
     */
    @GetMapping("carts")
    public ResponseData cartsList(HttpServletRequest request) {

        String userInfo = (String) request.getAttribute(TokenInterceptor.USER_INFO_KEY);
        JSONObject object = JSON.parseObject(userInfo);

        Long uid = Long.parseLong(object.get("uid").toString());
        CartListByIdRequest cartListByIdRequest = new CartListByIdRequest();
        cartListByIdRequest.setUserId(uid);

        CartListByIdResponse response = iCartService.getCartListById(cartListByIdRequest);
        if (response.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtil().setData(response.getCartProductDtos());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }


    /**
     * 购物车列表
     *
     * @param cartProductRequest 购物车请求
     * @param request            请求
     * @return {@link ResponseData}
     */
    @PutMapping("carts")
    public ResponseData cartList(@RequestBody CartProductRequest cartProductRequest, HttpServletRequest request) {

        UpdateCartNumRequest updateCartNumRequest = new UpdateCartNumRequest();
        updateCartNumRequest.setNum(cartProductRequest.getProductNum());
        updateCartNumRequest.setChecked(cartProductRequest.getChecked());
        updateCartNumRequest.setUserId(cartProductRequest.getUserId());
        updateCartNumRequest.setItemId(cartProductRequest.getProductId());


        UpdateCartNumResponse response = iCartService.updateCartNum(updateCartNumRequest);
        if (response.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtil().setData(response.getMsg());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }


    /**
     * 删除购物车条目
     *
     * @param userId 用户id
     * @param itemId 商品id
     * @return {@link ResponseData}
     */
    @RequestMapping("carts/{uid}/{pid}")
    public ResponseData deleteCartItem(@PathVariable("uid") Long userId, @PathVariable("pid") Long itemId) {

        DeleteCartItemRequest deleteCartItemRequest = new DeleteCartItemRequest();
        deleteCartItemRequest.setUserId(userId);
        deleteCartItemRequest.setItemId(itemId);

        DeleteCartItemResponse deleteCartItemResponse = iCartService.deleteCartItem(deleteCartItemRequest);
        if (ShoppingRetCode.SUCCESS.getCode().equals(deleteCartItemResponse.getCode())) {
            return new ResponseUtil().setData(deleteCartItemResponse);
        }
        return new ResponseUtil().setErrorMsg(deleteCartItemResponse.getMsg());
    }

    /**
     * 删除购物车的物品
     *
     * @param id id
     * @return {@link ResponseData}
     */
    @DeleteMapping("items/{id}")
    public ResponseData deleteCartItems(@PathVariable Long id) {

        DeleteCheckedItemRequest request = new DeleteCheckedItemRequest();
        request.setUserId(id);

        DeleteCheckedItemResposne resposne = iCartService.deleteCheckedItem(request);

        if (ShoppingRetCode.SUCCESS.getCode().equals(resposne.getCode())) {
            return new ResponseUtil().setData("成功");
        }

        return new ResponseUtil().setErrorMsg(resposne.getMsg());

    }

    @PutMapping("items")
    public ResponseData selectAll(@RequestBody Map<String,String> data, HttpServletRequest request) {
        String userId = data.get("userId");
        String checked = data.get("checked");
        CheckAllItemRequest checkAllItemRequest = new CheckAllItemRequest();
        checkAllItemRequest.setUserId(Long.parseLong(userId));
        checkAllItemRequest.setChecked(checked);

        CheckAllItemResponse response = iCartService.checkAllCartItem(checkAllItemRequest);

        if (response.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtil().setData(null);
        }

        return new ResponseUtil().setErrorMsg(response.getMsg());


    }

}
